<?php
require_once("../inc/bootstrap.php");

$filename = $_GET['fn'];
$filesize = $_GET['fs'];
$filehash = $_GET['fh'];
$ip = $_GET['ip'];
$agent = $_GET['agent'];
$title = $_GET['ft'];

/* Add a new upload and start sending out post download request to 'slave' nodes.
   Parameters: s=source server name, a(ction)=u(pload)|s(end), fn=file name, fh=file hash, fs=file size, ip=poster ip, ua=user agent, ft=file title (if provided).
   Return values: created post ID, 1 on success or 0 on error.
*/

    if ($servername != $SQLMaster)
    {
        echo "0";
        exit;
    }
    else
    {
        $mysqli = dbconnect();
        switch($_GET['a']) {
            case 'u':
global $filename, $filesize, $filehash, $ip, $agent, $title;
$query = "INSERT INTO uploads (filename,filesize,filehash,ip,user_agent,title) VALUES(?, ?, ?, ?, ?, ?)";
$statement = $mysqli->stmt_init();
if ($statement->prepare($query))
{
$statement->bind_param("ssssss", $filename, $filesize, $filehash, $ip, $agent, $title);
$statement->execute();
$ID = $statement->insert_id;
$statement->close();
$mysqli->close();
echo $ID;
} else {
error_log("Error during upload: $mysqli->error", 3, "$logsdir/error.log");
$statement->close();
$mysqli->close();
echo "0";
}
break;

            case 's':
$query = "SELECT hostname FROM nodes WHERE active=1 AND NOT hostname=?";
$statement = $mysqli->stmt_init();
if ($statement->prepare($query))
{
$statement->bind_param("s", $_GET['s']);
$statement->execute();
while($row = $mysqli->fetch_row()) 
{
$hostname = $row['0'];
$s = $_GET['s'];
$id = $_GET['id'];
file_get_contents("http://$hostname.$domain/api/get.php?s=$s&id=$id");
}
$statement->close();
$mysqli->close();
} else {
error_log("Error during transfer: $mysqli->error", 3, "$logsdir/error.log");
$statement->close();
$mysqli->close();
echo "File transfer not complete. See error log for more details.";
}
break;

            default:
error_log("Error: Invalid value given for 'action' parameter in upload.php", 3, "$logsdir/error.log");
echo "-1";
                break;
        }
    }
?>
